/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-10-23 19:01:31
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class InformationController extends Controller {
    // 获取房源数据
    async getInformation() {
        const { ctx } = this;
        const results = await this.app.mysql.select('information');
        const { value, state, pageCode, pageSize } = ctx.request.body;
        let newLists = results;
        if (value) {
            newLists = newLists.filter(v => v.title.includes(value))
        }
        if (state) {
            newLists = newLists.filter(v => v.status.includes(state))
        }
        let total = newLists.length;
        newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)
        if (results) {
            ctx.body = {
                code: 200,
                list: newLists,
                total,
                msg: '获取成功',
                results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }
    // 删除数据
    async delInformation() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('information', { id });
        if (result) {
            const results = await this.app.mysql.select('information');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败'
            }
        }

    }

    // 添加数据
    async addInformation() {
        const { ctx } = this;
        const { title,leader,updateTime} = ctx.request.body;
        const result = await this.app.mysql.insert('information', { title,leader,updateTime });
        if (result) {
            const results = await this.app.mysql.select('information');
            ctx.body = {
                code: 200,
                msg: '添加成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '添加失败'
            }
        }
    }
    // 编辑
    async editInformation() {
        const { ctx } = this;
        const { title, leader, updateTime,id } = ctx.request.body;
        const row = {
            title, leader, updateTime,
        };

        const options = {
            where: {
                id
            }
        };
        const result = await this.app.mysql.update('information', row, options);
        if (result) {
            ctx.body = {
                code: 200,
                msg: '修改成功',
                list: result
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '修改失败',
            }
        }
    }
    // 状态的改变
    async changeStatus(){
        const {ctx}=this;
        const {id,status}=ctx.request.body;
        // 关闭
        if(status==='开启'){
            const row = {
                status:'关闭'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('information', row, options); 
            if(result){
                ctx.body={
                    code:200,
                    msg:'关闭成功',
                }
            } else{
                ctx.body={
                    code:500,
                    msg:'关闭失败'
                }
            }
        } else if (status === '关闭') {
            // 开启
            const row = {
                status: '开启'
            };

            const options = {
                where: {
                    id
                }
            };
            const result = await this.app.mysql.update('information', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '开启成功',
                }
            } else {
                ctx.body = {
                    code: 500,
                    msg: '开启失败'
                }
            }
        }
    }
}

module.exports = InformationController;
